﻿//#define ToLua
using UnityEngine;
using System.Collections;
using System.Text;
using UnityEditor;
using System.Collections.Generic;

#if ToLua
using LuaInterface;
#endif 

public class BaseTestWindow : EditorWindow
{
    public static Dictionary<string, BaseTestWindow> WinDic = new Dictionary<string, BaseTestWindow>();

#if ToLua
    public static LuaState Lua;
#endif

    public static void OpenTestWinodw<T>() where T : BaseTestWindow
    {
        BaseTestWindow win = null;
        string title = typeof(T).Name;
        if (WinDic.ContainsKey(title))
        {
            win = WinDic[title];
        }
        if (win != null)
        {
            win.Close();
            win = null;
            WinDic.Remove(title);
            return;
        }

        win = EditorWindow.GetWindow<T>(false, title, false);
        if (WinDic.ContainsKey(title) == false)
            WinDic.Add(title, win);
        else
        {
            WinDic[title].Close();
            WinDic[title] = win;
        }
        win.Show();
    }



    public void CustomLuaGUI(string className)
    {
#if ToLua
        if (GUILayout.Button("加载Lua"))
        {
            Lua.DoFile(className);
            Debug.Log("刷新Lua");
        }

        if (Lua == null)
        {
            new LuaResLoader();
            Lua = new LuaState();
            LuaBinder.Bind(Lua);
            Lua.Start();
        }

        if (Lua != null)
        {
            Lua.Require(className);
            LuaFunction f = Lua.GetFunction(className + ".OnGUI");
            if (f != null)
                f.Call();
        }
#endif
    }

    void OnApplicationQuit()
    {
#if ToLua
        Lua.Dispose();
        Lua = null;
        Debug.Log("lua销毁");
#endif
    }


}